﻿using BLL;
using DocumentFormat.OpenXml.Presentation;
using DocumentFormat.OpenXml.Spreadsheet;
using Entity;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SysFramework;
using System.Data;
using System.Reflection.Emit;
using System.Security.Cryptography;
using System.Xml.Linq;
using Tools;
using YunProject.Controllers.Base;
using static System.Runtime.CompilerServices.RuntimeHelpers;


namespace YunProject.Controllers
{
    public class PlanController : BaseController
    {
        PlanBLL planBLL;
        SystemManagement sysBLL;

        public PlanController()
        {
            planBLL = new PlanBLL();
            sysBLL = new SystemManagement();
        }
        #region
        [Authorize]
        public IActionResult PlanInfo()
        {
            return View();
        }
        [Authorize]
        public IActionResult UpdatePlan()
        {
            return View();
        }
        [Authorize]
        public IActionResult UpdatePlanNew()
        {
            return View();
        }
        [Authorize]
        public IActionResult PlanProcess()
        {
            return View();
        }
        [Authorize]
        public IActionResult SalesInfo()
        {
            return View();
        }
        [Authorize]
        public IActionResult RecentPlanInfo()
        {
            return View();
        }
        [Authorize]
        public IActionResult UpdatePlanGY()
        {
            return View();
        }
        /// <summary>
        /// 计划运费
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public IActionResult PlanFreight()
        {
            return View();
        }
        [Authorize]
        public IActionResult AddPlanFreight()
        {
            return View();
        }
        /// <summary>
        /// 原辅料占用
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public IActionResult MaterialOccupy()
        {
            return View();
        }
        [Authorize]
        public IActionResult UploadPlanFile()
        {
            return View();
        }
        [Authorize]
        public IActionResult MOccupyWithOutBar()
        {
            return View();
        }

        [Authorize]
        public IActionResult YinShuaForm()
        {
            return View();
        }
        [Authorize]
        public IActionResult ZhiDaiForm()
        {
            return View();
        }
        [Authorize]
        public IActionResult FenQieForm()
        {
            return View();
        }
        
        [Authorize]
        public IActionResult FenQie1Form()
        {
            return View();
        }
        [Authorize]
        public IActionResult CPFenQieForm()
        {
            return View();
        }
        [Authorize]
        public IActionResult FuHeForm()
        {
            return View();
        }

        [Authorize]
        public IActionResult WRJFuHeForm()
        {
            return View();
        } 
        
        [Authorize]
        public IActionResult GSFuHeForm()
        {
            return View();
        }

        /// <summary>
        /// 存货明细
        /// </summary>
        /// <returns></returns>
        [Authorize]
        public IActionResult PlanStockDetail()
        {
            return View();
        }

        [Authorize]
        public IActionResult PlanMaterialUse()
        {
            return View();
        }

        [Authorize]
        public IActionResult pMUseEdit()
        {
            return View();
        }

        [Authorize]
        public IActionResult WasteEdit()
        {
            return View();
        }
        [Authorize]
        public IActionResult PlanDetail()
        {
            return View();
        }

        [Authorize]
        public IActionResult purchaseApplyList()
        {
            return View();
        }
        [Authorize]
        public IActionResult FreightList()
        {
            return View();
        }
        [Authorize]
        public IActionResult FreightDetail()
        {
            return View();
        }

        [Authorize]
        public IActionResult PPMList()
        {
            return View();
        }
        #endregion


        [HttpGet]
        public JsonResult getDataList(string page, string limit, string condition, string dataType)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            string pageCount = "1";
            string msg = "";
            var datas = new object();
            if (dataType == "plan")
            {
                rsl = planBLL.getPlanList(userObj, page, limit, ref pageCount, condition);//生产计划
            }
            else if (dataType == "RecentPlan")
            {
                rsl = planBLL.getPlanList(userObj, page, limit, ref pageCount, condition, dataType);//往期生产计划
            }
            else if (dataType == "sale")
            {
                rsl = planBLL.getSalesInfo(userObj, condition, "", "");//销售订单
            }
            else if (dataType == "pml")
            {
                rsl = planBLL.GetProductBom(page, limit, condition, ref pageCount, userObj["baseCode"].ToString());
            }
            else if (dataType == "mstock")
            {
                rsl = planBLL.GetBomStock(page, limit, condition, ref pageCount, userObj["baseCode"].ToString());
            }
            else if (dataType == "cgList")
            {
                rsl = planBLL.PurchaseList(page, limit, condition, ref pageCount);   //计划待采购清单
            }
            else if (dataType == "gxlist")
            {
                rsl = planBLL.GetGongXuList(condition);
            }
            else if (dataType == "pzyl")
            {
                rsl = planBLL.GetPlanZhanYong(page, limit, condition, ref pageCount);
            }
            else if (dataType == "pgyb")
            {
                rsl = planBLL.GetBanGunByPlCode(condition);
            }
            else if (dataType == "sdt")
            {
                rsl = planBLL.StockDetail(page, limit, condition, ref pageCount, userObj["baseCode"].ToString());//现存量明细
            }
            else if (dataType == "pMUse")
            {
                rsl = planBLL.getPlanDoingList(userObj, page, limit, ref pageCount, condition);//计划列表(录入计划用料使用)
            }
            else if (dataType == "planyl")
            {
                rsl = planBLL.getPlanYongLiaoList(page, limit, ref pageCount, condition, userObj["baseCode"].ToString());//计划用料
            }
            else if (dataType == "planfl")
            {
                rsl = planBLL.getPlanWasteList(condition);//计划废料
            }
            else if (dataType == "file")
            {
                rsl = sysBLL.getUploadFiles(condition);//计划附件
            }
            else if (dataType == "glgv")
            {
                rsl = planBLL.PlanProcessProduction(condition);//计划工序生产统计
            }
            else if (dataType == "pWaste")
            {
                rsl = planBLL.GetPlanWaste(page, limit, condition, ref pageCount);//计划废料
            }
            else if (dataType == "psfl")
            {
                rsl = planBLL.GetSaleOutProductList(condition);
            }
            else if (dataType == "sflist")
            {
                rsl = planBLL.GetFerightList(page, limit, condition, ref pageCount);
            }
            else if (dataType == "sflde")
            {
                rsl = planBLL.FreightDetail(condition);
            }
            else if (dataType == "purchase")
            {
                rsl = planBLL.getTaskCenter(page, limit, ref pageCount, condition);
            }
            else if (dataType == "gxzym")
            {
                rsl = planBLL.FHGongYiMaterial(condition);
            }
            else if (dataType == "ppml")
            {
                rsl = planBLL.GetPPMList(condition);
            }
            


            if (rsl.Result)
            {
                datas = rsl.Data;
            }
            else
            {
                msg = rsl.Message;
            }

            var Data = new
            {
                count = pageCount,
                msg = msg,
                code = "0",
                data = datas
            };

            return new JsonResult(Data);
        }

        [HttpPost]
        public JsonResult DelPlan(string plCode, string status)
        {
            var userObj = LoginInfo();
            return Json(planBLL.DelPlan(userObj, plCode, status));
        }
        [HttpPost]
        public JsonResult SaveShengLiao(string dataId, string value)
        {
            var userObj = LoginInfo();
            return Json(planBLL.SaveShengLiao(dataId,value));
        }

        
        [HttpPost]
        public JsonResult SaveProductPlan(string datas, string cData)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.SaveProductPlan(datas, cData, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult DelFreightInfo(string ftCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.DelFreightInfo(ftCode, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult DelPPMList(string dataId)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.DelPPMList(dataId, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        

        [HttpPost]
        public JsonResult AddPlanFreight(string mCodes, string datas, string memo)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.AddPlanFreight(mCodes, datas, memo, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult DelPlanZhanYong(string dataId)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.DelPlanZhanYong(dataId, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }






        /// <summary>
        /// 修改时 获取计划信息
        /// </summary>
        /// <param name="keyCode"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult getPlanInfoByCode(string plCode)
        {
            return Json(planBLL.getPlanInfoByCode(plCode));
        }



        /// <summary>
        /// 修改时 获取计划工序信息
        /// </summary>
        /// <param name="keyCode"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult getPlanGxByCode(string plCode)
        {
            return Json(planBLL.getPlanGxByCode(plCode));
        }



        [HttpPost]
        public JsonResult GetPlanProcess(string plCode, string gyCode)
        {
            return Json(planBLL.GetPlanProcess(plCode, gyCode));
        }
        [HttpPost]
        public JsonResult GetPlanGx(string plCode)
        {
            return Json(planBLL.GetPlanGx(plCode));
        }   
        [HttpPost]
        public JsonResult loadPlanGxForm(string datas)
        {
            return Json(planBLL.loadPlanGxForm(datas));
        }
        [HttpPost]
        public JsonResult DelPlanGongYi(string plCode, string gxCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.DelPlanGongYi(plCode, gxCode, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult SavePlanMeterial(string plCode, string cDatas, string numType, string gxCode, string gxName)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.SavePlanMeterial(plCode, cDatas, numType, userObj["userCode"].ToString(), gxCode, gxName);
            }
            return Json(rsl);
        }



        [HttpPost]
        public JsonResult GetProductGyCode(string proCode, string cuCode)
        {
            var rsl = planBLL.GetProductGyCode(proCode, cuCode);

            return Json(rsl);
        }

        [HttpPost]
        public JsonResult GetProductBOMCode(string proCode)
        {
            BasicBLL basic = new BasicBLL();
            var rsl = basic.GetProductBOMCode(proCode);

            return Json(rsl);
        }

        [HttpPost]
        public JsonResult GetBgByBOM(string bomCode)
        {
            BasicBLL basic = new BasicBLL();
            var rsl = basic.GetBgByBOM(bomCode);

            return Json(rsl);
        }

        [HttpPost]
        public JsonResult SavePurchaseApply(string plCode, string cdatas, string datas)
        {
            var userObj = LoginInfo();
            var rsl = planBLL.SavePurchaseApply(plCode, userObj["userCode"].ToString(), userObj["baseCode"].ToString(), cdatas, datas);

            return Json(rsl);
        }

        [HttpPost]
        public JsonResult SavePlanTechnology(string datas)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.SavePlanTechnology(datas, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult GetPlanTechnologyForm(string plCode, string gxCode)
        {

            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.GetPlanTechnologyForm(plCode, gxCode);
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult PlanPaiGong(string datas)
        {
            var rsl = planBLL.PlanPaiGong(datas);

            return Json(rsl);
        }

        [HttpPost]
        public JsonResult PlanCancelPaiGong(string datas)
        {
            var rsl = planBLL.PlanCancelPaiGong(datas);

            return Json(rsl);
        }

        [HttpPost]
        public JsonResult getProUprice(string proCode, string batch)
        {
            var rsl = planBLL.getProUprice(proCode, batch);
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult savePlanMaterials(string datas)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.savePlanMaterials(datas);
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult updatePlanMaterials(string datas)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.updatePlanMaterials(datas, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult DelPlanMaterials(string dataId)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.DelPlanMaterials(dataId, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult finishPlanGx(string plCode, string gxCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.FinishPlanGx(plCode, gxCode, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult finishPlan(string datas)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.FinishPlan(datas, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult AddPlanWaste(string datas)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.AddPlanWaste(datas, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult DelPlanWaste(string dataId)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.DelPlanWaste(dataId, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }
        [HttpPost]
        public JsonResult GongYiMaterial(string plCode, string gxCode)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.GongYiMaterial(plCode, gxCode);
            }
            return Json(rsl);
        }
        #region 此方法暂时注销不用
        //[HttpPost]
        //public JsonResult FHGongYiMaterial(string plCode, string gxCode)
        //{
        //    var rsl = new ReturnResult();
        //    var userObj = LoginInfo();
        //    if (userObj != null)
        //    {
        //        rsl = planBLL.FHGongYiMaterial(plCode, gxCode);
        //    }
        //    return Json(rsl);
        //}
        #endregion
        [HttpPost]
        public JsonResult SavePlanWasteAsRd08(string datas)
        {
            var rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.SavePlanWasteAsRd08(datas, userObj["userCode"].ToString(), userObj["baseCode"].ToString(), userObj["dept"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult UploadFiles(string plCode)
        {
            ReturnResult rsl = new ReturnResult();

            var s = Request.Form.Files;
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = sysBLL.UploadFile("Plan", plCode, s, userObj["userCode"].ToString());
            }
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult delUploadFiles(string id, string fileName)
        {
            ReturnResult rsl = sysBLL.delUploadFiles(id, fileName);
            return Json(rsl);
        }

        [HttpPost]
        public JsonResult ConfirmTask(string ids)
        {
            ReturnResult rsl = new ReturnResult();
            var userObj = LoginInfo();
            if (userObj != null)
            {
                rsl = planBLL.taskConfirm(ids, userObj["userCode"].ToString());
            }
            return Json(rsl);
        } 
        
        [HttpPost]
        public JsonResult addPurchaseList(string plCode,string field, string datas)
        {
            var rsl = planBLL.addPurchaseList(plCode, field, datas);
            return Json(rsl);
        }  
        
        [HttpPost]
        public JsonResult DelPurchaseList(string id)
        {
            var rsl = planBLL.DelPurchaseList(id);
            return Json(rsl);
        }

        [HttpGet]
        public FileResult gyPrint(string plCode, string gxCode)
        {
            var FileName = "";
            var ms = planBLL.gyPrintToExcel(plCode, gxCode,ref FileName);
            return File(ms, "application/vnd.ms-excel",FileName);

        }

    }
}
